package com.yn.xgame.util;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

/**
 * CtxUtils
 * Created by wangboo on 17/7/13.
 */
public class CtxUtils {

    private static final AttributeKey<Long> idKey = AttributeKey.newInstance("id");
    private static final AttributeKey<String> logicTopicKey = AttributeKey.newInstance("logicTopic");

    /**
     * 获取session的id编号
     * @return -1 表示没有编号
     */
    public static long getId(ChannelHandlerContext ctx) {
        Long v = ctx.attr(idKey).get();
        if (v == null) return -1L;
        return v;
    }

    public static void setId(ChannelHandlerContext ctx, long id) {
        ctx.attr(idKey).set(id);
    }


    public static String getLogicTopic(ChannelHandlerContext ctx) {
        return ctx.attr(logicTopicKey).get();
    }

    public static void setLogicTopic(ChannelHandlerContext ctx, String topic) {
        ctx.attr(logicTopicKey).set(topic);
    }
}
